package com.wang.fastdfs.controller;

import com.wang.fastdfs.utils.FastdfsUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author: wang
 * @date: 2021/7/29 20:22
 * @description:
 */
@Controller
public class FastdfsController {


    @RequestMapping("/")
    public String index(){
        return "index";
    }

    @RequestMapping("upload")
    public String upload(MultipartFile file, Model m) throws IOException {
        if (file == null)
            throw new FileNotFoundException("文件上传失败");
        String name = file.getOriginalFilename();
        String suffix = name.substring(name.lastIndexOf(".") + 1);
        String path = FastdfsUtil.fileUpload(file.getBytes(), suffix);
        System.out.println(path);
        m.addAttribute("path",path);
        return "show";
    }
}
